/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.informet.ipd.modelo.dao;

import com.informet.ipd.modelo.vo.CMObjetivoVO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author LAPTOP01
 */
public class CMObjetivoDao extends DBase {

    Connection conn = null;

    public CMObjetivoDao()  {
        super();
    }

    private void setCMObjetivo(CMObjetivoVO bean, ResultSet rs) throws SQLException {
        bean.setNUM_ANIOMETAO_PK(rs.getInt("NUM_ANIOMETAO_PK"));
        bean.setNUM_CODMETAO_PK(rs.getInt("NUM_CODMETAO_PK"));
        bean.setNUM_CUMETAO(rs.getInt("NUM_CUMETAO"));
        bean.setNUM_IDCUMETAO(rs.getInt("NUM_IDCUMETAO"));
        bean.setNUM_IDMOBJ(rs.getInt("NUM_IDMOBJ"));
        bean.setNUM_IDOBJ(rs.getInt("NUM_IDOBJ"));
    }

    public String spMan(int tipo, CMObjetivoVO bean) {
        String msj = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            CallableStatement cs = conn.prepareCall("{call Man_CUMETAOBJ(?,?,?,?,?)}");
            cs.setInt(1, tipo);
            cs.setInt(2, bean.getNUM_ANIOMETAO_PK());
            cs.setInt(3, bean.getNUM_CUMETAO());
            cs.setInt(4, bean.getNUM_CODMETAO_PK());
            cs.setInt(5, bean.getNUM_IDCUMETAO());
            cs.execute();



        } catch (Exception e) {
            msj = e.toString();
            return msj;
        }
        return msj;
    }

    public ArrayList<CMObjetivoVO> selectLista() throws SQLException {

        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<CMObjetivoVO> lista = new ArrayList<CMObjetivoVO>(0);
        //lista=null;
        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            stmt = conn.createStatement();
            sql = "select NUM_ANIOMETAO_PK,NUM_CUMETAO from ME_CUMETAOBJ";
            rs = stmt.executeQuery(sql);
            CMObjetivoVO bean = null;
            while (rs.next()) {
                bean = new CMObjetivoVO();
                setCMObjetivo(bean, rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }

    public List getlist(int cod) throws SQLException {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ArrayList<CMObjetivoVO> lista = new ArrayList<CMObjetivoVO>(0);
        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            sql = "select cmo.NUM_CUMETAO,cmo.NUM_IDCUMETAO,cmo.NUM_ANIOMETAO_PK,cmo.NUM_CODMETAO_PK, obj.NUM_IDOBJ,mobj.NUM_IDMOBJ from ME_METAOBJ mobj inner join ME_OBJ obj on obj.NUM_CODOBJ_PK=mobj.NUM_CODOBJ_PK inner join ME_CUMETAOBJ cmo on cmo.NUM_CODMETAO_PK=mobj.NUM_CODMETAO_PK where cmo.NUM_CODMETAO_PK=? order by NUM_IDCUMETAO";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, cod);
            rs = pstmt.executeQuery();

            CMObjetivoVO bean = null;
            while (rs.next()) {
                bean = new CMObjetivoVO();
                setCMObjetivo(bean, rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }

    public CMObjetivoVO selectById(int id) throws SQLException {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = null;
        CMObjetivoVO bean = null;
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            String sql = "select NUM_ANIOMETAO_PK,NUM_CUMETAO from ME_CUMETAOBJ where NUM_ANIOMETAO_PK=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();
            bean = new CMObjetivoVO();
            if (rs.next()) {
                setCMObjetivo(bean, rs);
            }
            rs.close();
            pstmt.close();
        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }

        return bean;
    }
}
